package cn.mogugo.util.tools;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;


public class FileTools {
	
	public static void main(String[] args){
		FileTools ft = new FileTools();
		for(int i = 0 ; i < 10 ; i ++ )
			System.out.println(ft.changeFileNameByTime("1.txt"));	
			
	}
	
	
	public static String changeFileNameByTime(String fileName) {
		String subffix = fileName.substring(fileName.lastIndexOf("."));
		// 为附件重新命名并混淆
		String fn = DateTools.getCurTime().toString() + Random.getStrRandom(5);
		int size = fn.length();
		if(size < 18){
			fn += Random.getStrRandom(18-size);
		}else if(size > 18){
			fn = fn.substring(0, 17);
		}
		char[] strChar = fn.toCharArray();
		String newFileName = "";
		for(int i = 0 ; i < size ; i=i+3){
			int asc = strChar[i] + strChar[i+1] + strChar[i+2];
			if(asc % 3 == 0){
				newFileName += Random.getNumberRandom();
			}else if(asc % 3 == 1){
				newFileName += Random.getULetterRandom();
			}else if(asc % 3 == 2){
				newFileName += Random.getLetterRandom();
			}
			
		}
		newFileName += subffix;
		return newFileName;
	}
	
	
	public static void uploadFile(InputStream in,String newFilePath ,String newFileName) {

		byte[] buffer = new byte[8192];
		OutputStream out = null;
		try {
			File fileDir = new File(newFilePath);
			
			
			
			if(!fileDir.exists()){
				fileDir.mkdir();
			}
			buffer = new byte[8192];
			out = new FileOutputStream(newFilePath + File.separator + newFileName);

			int bytesRead = 0;
			while ((bytesRead = in.read(buffer, 0, 8192)) != -1) {
				out.write(buffer, 0, bytesRead);// 将文件写入服务器
			}
			out.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
			try {
				out.close();
				in.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			}

		}
	}

	
	public static void saveTextToFile(String filePath , String text){
		File file = createNewFile(filePath);
		
		try{
			FileOutputStream fw = new FileOutputStream(file);
	        fw.write(text.getBytes());
	        fw.flush();
	        fw.close();
		}catch(Exception e){
			e.printStackTrace();
		}

	}
	
	/**
	 * 根据文件名来创建文件对象
	 * @param filePath
	 * @return
	 */
	private static File createNewFile(String filePath){
		File file = null;
		try{
			file = new File(filePath);
			if(!file.exists()){
				int flag = filePath.lastIndexOf(File.separator);
				String dirPath = filePath.substring(0, flag);
				File dir = new File(dirPath);
				dir.mkdirs();
				file.createNewFile();
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return file;
	}
	
	public static int unZipFile(String zipFileName, String outputDirectory) {
		int unzipFileNumber = 0;
		try {
			org.apache.tools.zip.ZipFile zipFile = new org.apache.tools.zip.ZipFile(
					outputDirectory + zipFileName);
			java.util.Enumeration e = zipFile.getEntries();
			org.apache.tools.zip.ZipEntry zipEntry = null;
			createDirectory(outputDirectory, "");
			while (e.hasMoreElements()) {
				unzipFileNumber++;
				zipEntry = (org.apache.tools.zip.ZipEntry) e.nextElement();
				if (zipEntry.isDirectory()) {
					String name = zipEntry.getName();
					name = name.substring(0, name.length() - 1);
					File f = new File(outputDirectory + File.separator + name);
					f.mkdir();
				} else {
					String fileName = zipEntry.getName();
					fileName = fileName.replace('\\', '/');
					if (fileName.indexOf("/") != -1) {
						createDirectory(outputDirectory, fileName.substring(0,
								fileName.lastIndexOf("/")));
						fileName = fileName.substring(
								fileName.lastIndexOf("/") + 1, fileName
										.length());
					}

					File f = new File(outputDirectory + File.separator
							+ zipEntry.getName());

					f.createNewFile();
					InputStream in = zipFile.getInputStream(zipEntry);
					FileOutputStream out = new FileOutputStream(f);

					byte[] by = new byte[1024];
					int c;
					while ((c = in.read(by)) != -1) {
						out.write(by, 0, c);
					}
					try {
						out.close();
						in.close();
					} catch (Exception ee) {
						ee.printStackTrace();
					}
				}
			}
			zipFile.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return unzipFileNumber;
	}

	public static void createDirectory(String directory, String subDirectory) {
		String dir[];
		File fl = new File(directory);
		try {
			if (subDirectory == "" && fl.exists() != true)
				fl.mkdir();
			else if (subDirectory != "") {
				dir = subDirectory.replace('\\', '/').split("/");
				for (int i = 0; i < dir.length; i++) {
					File subFile = new File(directory + File.separator + dir[i]);
					if (subFile.exists() == false)
						subFile.mkdir();
					directory += File.separator + dir[i];
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void deleteFile(String fileName) {
		File afile = new File(fileName);
		afile.delete();
	}

	public static boolean deleteDir(File f) {
		boolean rtn = true;
		if (f.isDirectory()) {
			File[] files = f.listFiles();
			for (int i = 0; i < files.length; i++) {
				if (files[i].isDirectory()) {
					deleteDir(files[i]);
				} else {
					rtn = files[i].delete();
					if(!rtn){
						break;
					}
				}
			}
			if(rtn){
				rtn = f.delete();
			}
		}else{
			rtn = f.delete();
		}
		return rtn;
	}

	/**
	 * 对比文件名的后缀是否与指定的后缀一致
	 * @param arg
	 * @param suffix
	 * @return
	 */
	public static boolean checkSuffix(String arg , String suffix){
		String[] str = arg.split("\\.");
		if(str == null || str.length <= 1){
			return false;
		}else{
			String sf = str[str.length - 1];
			if(sf.toLowerCase().equals(suffix.toLowerCase())){
				return true;
			}else{
				return false;
			}
		}
	}
	
	/**
	 * 剥离出文章名
	 * @return
	 */
	public static String getFileNameOutOfSuffix(String fileName){
		int length = fileName.lastIndexOf(".");
		return fileName.substring(0, length);
	}
}
